package com.x.ant.manage;

import com.alibaba.dubbo.config.annotation.Reference;
import com.x.ant.dao.LineDao;
import com.x.ant.model.*;
import com.x.framework.manage.BaseManage;
import com.x.security.model.Account;
import com.x.security.rpc.SecurityRpcService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class TruckManage extends BaseManage {
    //    @Autowired
//    private LineDao lineDao;
    @Reference(interfaceName="com.x.security.rpc.SecurityRpcService")
    private SecurityRpcService securityRpcService;

    public Truck insertTruck(Truck truck, String deptId, String deptParentId) throws Exception {
        User user = truck.getUser();
        String userId = user.getUserId();
        if (!isString(userId)) {
            user.setUserCreateTime(this.getDate(0));
            user = this.insert(user);
            userId = user.getUserId();
        } else {
            this.update(user);
        }

        truck.setUserId(userId);
        String truckId = truck.getTruckId();
        if (!isString(truckId)) {
            truck.setTruckCreateTime(this.getDate(0));
            truck = this.insert(truck);
        } else {
            this.update(truck);
        }

        DeptUser deptUser = new DeptUser();
        deptUser.setDeptId(deptId);
        deptUser.setDeptParentId(deptParentId);
        deptUser.setUserId(userId);
        this.delete(deptUser);
        this.insert(deptUser);

        Map<String, String> accountPropertyMap = new HashMap<String, String>();
        accountPropertyMap.put("ACCOUNT_NAME", truck.getTruckMobile());
        accountPropertyMap.put("userId", userId);
        Account account = new Account();
        account.setAccountName(truck.getTruckMobile());
        account.setAccountPassword(truck.getTruckMobile().substring(5));
        account.setAccountRemark("黑蚁司机" + user.getUserName());
        account.setAccountState(0);
        account.setAccountCreateTime(this.getDate(0));
        account.setAccountModifyTime(this.getDate(0));
        account.setAccountPropertyMap(accountPropertyMap);
        account = securityRpcService.insertAccount(account, "13927");

        return truck;
//        lineDao.updateLineTruckNum(line);
    }

}